package com.newbingo.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.newbingo.dao.imple.IBettingItemDao;
import com.newbingo.dao.imple.IPeriodGameDao;
import com.newbingo.dto.PeriodHistoryDto;
import com.newbingo.dto.imple.IPeriodHistoryDto;
import com.newbingo.entity.BettingCell;
import com.newbingo.entity.BettingItem;
import com.newbingo.entity.GamePlay;
import com.newbingo.entity.Lottery;
import com.newbingo.entity.PeriodGame;
import com.newbingo.entity.PeriodGameResult;
import com.newbingo.service.imple.IPeriodGameService;
import com.newbingo.util.Pager;


public class PeriodGameService implements IPeriodGameService {
	private IPeriodGameDao periodDao = null;
	private IPeriodHistoryDto dto=null;
	private IBettingItemDao bettingItemDao;

	public void attachDirty(PeriodGame instance) {
		periodDao.attachDirty(instance);

	}

	/**
	 * 删除
	 */
	public void delete(PeriodGame persistentInstance) {
		periodDao.delete(persistentInstance);
	}

	/**
	 * 查询所有记录
	 * 
	 * @return List
	 */
	public List findAll() {
		return periodDao.findAll();
	}

	/**
	 * 查询
	 * 
	 * @return List
	 */
	public List findByExample(PeriodGame instance) {
		return periodDao.findByExample(instance);
	}

	/**
	 * 根据GameTable 编号查询PeriodGame
	 * 
	 * @return pager
	 * @param start
	 * @param limit
	 * @param gtno
	 */
	public Pager findByGtNO(int start, int limit, int gtno) {
		return periodDao.findByGtNO(start, limit, gtno);
	}

	/**
	 * 根据编号查询
	 * 
	 * @return periodGame
	 * @param id
	 */
	public PeriodGame findById(Integer id) {
		return periodDao.findById(id);
	}

	/**
	 * 分页查询
	 * 
	 * @return Pager
	 * @param start
	 * @param limit
	 * @param type
	 */
	public Pager findByPage(int start, int limit, Object type) {
		return periodDao.findByPage(start, limit, type);
	}

	/**
	 * 根据期号查询
	 * 
	 * @return List
	 * @param periodNum
	 */
	public List findByPeriodNum(Object periodNum) {
		return periodDao.findByPeriodNum(periodNum);
	}

	/**
	 * 根据PeriodTime查询
	 * 
	 * @return List
	 * @param periodTime
	 */
	public List findByPeriodTime(Object periodTime) {
		return periodDao.findByPeriodTime(periodTime);
	}

	/**
	 * 根据属性查询
	 * 
	 * @return List
	 */
	public List findByProperty(String propertyName, Object value) {
		return periodDao.findByProperty(propertyName, value);
	}

	/**
	 * 修改
	 */
	public PeriodGame merge(PeriodGame detachedInstance) {
		return periodDao.merge(detachedInstance);
	}

	/**
	 * 保存
	 */
	public void save(PeriodGame transientInstance) {
		periodDao.save(transientInstance);

	}
	
	
	/**
	 * 查询游戏历史开奖记录
	 * @param gtno 游戏桌编号
	 * @param date 时间
	 * @return Pager
	 * */
    public  Pager findHistoryRecord(int start,int limit,int gtno,String date){
    	return dto.changeToString(start, limit, gtno, date);
    }
	
	
	public PeriodGame getCurrentPeriodGame(int gtno){
		return periodDao.getCurrentPeriodGame(gtno);
	}
	
	/**
	 * 保存本期游戏开奖号码
	 * @param gtno
	 */
	public void savePeriodGameResult(int gtno,int [] lotteries) {
		//根据游戏桌编号参数获取出本期游戏的对象
		PeriodGame periodGame=getCurrentPeriodGame(gtno);
		System.out.println("本期游戏为"+periodGame.getPgnum()+"\t编号为"+periodGame.getPgno());
		PeriodGameResult pgResult = periodGame.getPeriodGameResult();
//		System.out.println(periodGame.getGameTable().getName());
		//将球号转换为集合,并保存到本期游戏中
		List<Lottery> lotteries_list=Lottery.toLotteries(lotteries);
		pgResult.setLotteries(lotteries_list);
		//根据本期游戏获取对应的游戏桌上所有玩法
		Set<GamePlay> plays=periodGame.getGameTable().getGame().getPlays();
		//保存本期游戏为已开奖
		periodGame.setIsDraw(1);
		Set<BettingCell> cells=new HashSet<BettingCell>();
		//将中奖的投注格保存到结果中
		for(GamePlay temp:plays){
			try{
				BettingCell bc = temp.getWinningCell(lotteries_list);
				cells.add(bc);
				pgResult.setCells(cells);
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		this.save(periodGame);
//		//获取本期的投注项中的所有信息
		List<BettingItem> bettingItems=bettingItemDao.findBettingItemsByPeriodGame(periodGame.getPgno());
		System.out.println(bettingItems.size());
		if(bettingItems!=null && bettingItems.size()>0){
			for(BettingItem bettingItem:bettingItems){
				if(cells.contains(bettingItem.getBettingCell())){
					bettingItem.setWin(1);
				}else{
					bettingItem.setWin(0);
				}
				bettingItem.getPayout();
			}
		}
	}

	public IPeriodHistoryDto getDto() {
		return dto;
	}

	public void setDto(IPeriodHistoryDto dto) {
		this.dto = dto;
	}
	
	public IBettingItemDao getBettingItemDao() {
		return bettingItemDao;
	}

	public void setBettingItemDao(IBettingItemDao bettingItemDao) {
		this.bettingItemDao = bettingItemDao;
	}

	public IPeriodGameDao getPeriodDao() {
		return periodDao;
	}

	public void setPeriodDao(IPeriodGameDao periodDao) {
		this.periodDao = periodDao;
	}
	
}
